package com.RedRiver.hospitalsystem.finance_service.service.impl;

import com.RedRiver.hospitalsystem.finance_service.common.exception.BillException;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.BillMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.DailyMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.InvoiceMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Bill;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Daily;
import com.RedRiver.hospitalsystem.finance_service.service.DailyService;
import com.RedRiver.hospitalsystem.finance_service.service.from.ChargeFrom;
import com.RedRiver.hospitalsystem.finance_service.service.info.ChargeAllInfo;
import com.RedRiver.hospitalsystem.finance_service.service.info.ChargeRefundInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Resource
    private BillMapper billMapper;
    @Resource
    private InvoiceMapper invoiceMapper;
    @Override
    public ChargeAllInfo chargeAll(ChargeFrom copyone) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        if (copyone.getStart()!=null&&copyone.getStop()!=null){
            wrapper.between("bill_time",copyone.getStart(),copyone.getStop());
        }
        wrapper.eq("bill_state",1);
        Double medicineMoney = 0.000;
        Double checkMoney = 0.000;
        Double serialMoney = 0.000;
        List<Bill> list = billMapper.selectList(wrapper);
        if (list!=null&&list.size()>0){
            for (Bill bill : list) {
                if (bill.getPaymentId()==1){
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    medicineMoney=medicineMoney+v;
                }else if (bill.getPaymentId()==2){
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    checkMoney=checkMoney+v;
                }else {
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    serialMoney=serialMoney+v;
                }
            }
        }else throw new BillException(50002,"该时间段未找到消费订单");
        ChargeAllInfo chargeAllInfo = new ChargeAllInfo();
        chargeAllInfo.setMedicineMoney(medicineMoney);
        chargeAllInfo.setCheckMoney(checkMoney);
        chargeAllInfo.setSerialMoney(serialMoney);
        return chargeAllInfo;
    }

    @Override
    public ChargeAllInfo refundAll(ChargeFrom copyone) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        if (copyone.getStart()!=null&&copyone.getStop()!=null){
            wrapper.between("refund_time",copyone.getStart(),copyone.getStop());
        }
        wrapper.eq("bill_state",2);
        Double medicineMoney = 0.000;
        Double checkMoney = 0.000;
        Double serialMoney = 0.000;
        List<Bill> list = billMapper.selectList(wrapper);
        if (list!=null&&list.size()>0){
            for (Bill bill : list) {
                if (bill.getPaymentId()==1){
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    medicineMoney=medicineMoney+v;
                }else if (bill.getPaymentId()==2){
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    checkMoney=checkMoney+v;
                }else {
                    BigDecimal bi = bill.getBillPrice();
                    Double v = bi.doubleValue();
                    serialMoney=serialMoney+v;
                }
            }
        }else throw new BillException(50002,"该时间段未找到退费订单");
        ChargeAllInfo chargeAllInfo = new ChargeAllInfo();
        chargeAllInfo.setMedicineMoney(medicineMoney);
        chargeAllInfo.setCheckMoney(checkMoney);
        chargeAllInfo.setSerialMoney(serialMoney);
        return chargeAllInfo;
    }

    @Override
    public ChargeRefundInfo riJieAll(ChargeFrom copyone) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        QueryWrapper<Bill> wrapper1 = new QueryWrapper<>();
        if (copyone.getStart()!=null&&copyone.getStop()!=null){
            wrapper.between("bill_time",copyone.getStart(),copyone.getStop());
            wrapper1.between("refund_time",copyone.getStart(),copyone.getStop());
        }
        Double medicineMoney = 0.000;
        Double checkMoney = 0.000;
        Double serialMoney = 0.000;
        Double outMedicineMoney = 0.000;
        Double outCheckMoney = 0.000;
        Double outSerialMoney = 0.000;
        List<Bill> bills = billMapper.selectList(wrapper);
        List<Bill> billss = billMapper.selectList(wrapper1);
        if (bills!=null&&bills.size()>0){
            for (Bill bill : bills) {
                if (bill.getBillState()==1){
                    if (bill.getPaymentId()==1){
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        medicineMoney=medicineMoney+v;
                    }else if (bill.getPaymentId()==2){
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        checkMoney=checkMoney+v;
                    }else {
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        serialMoney=serialMoney+v;
                    }
                }
            }
        }
        if (billss!=null&&billss.size()>0){
            for (Bill bill : billss) {
                if (bill.getBillState()==2){
                    if (bill.getPaymentId()==1){
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        outMedicineMoney=outMedicineMoney+v;
                    }else if (bill.getPaymentId()==2){
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        outCheckMoney=outCheckMoney+v;
                    }else {
                        BigDecimal bi = bill.getBillPrice();
                        Double v = bi.doubleValue();
                        outSerialMoney=outSerialMoney+v;
                    }
                }
            }
        }
        ChargeRefundInfo info = new ChargeRefundInfo();
        info.setCheckMoney(checkMoney);
        info.setMedicineMoney(medicineMoney);
        info.setSerialMoney(serialMoney);
        info.setOutMedicineMoney(outMedicineMoney);
        info.setOutCheckMoney(outCheckMoney);
        info.setOutSerialMoney(outSerialMoney);
        return info;
    }
}
